<?php 
session_start();

echo '<html>';
include 'includes/header-one.php'; ?>
<body>
<?php include 'includes/header-two.php'; 

mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS); //(host, username, password)
mysql_select_db(MYSQL_DB) or die("Unable to select database"); //select which database we're using

if ((isset($_GET['edit'])) && (isset($_GET['submit']))) {
        $query= "UPDATE ".URL_TABLE." SET url = '".$_POST['longurl']."', disabled='".(($_POST['disabled']=='1') ? '1' : '0')."' WHERE id = '".$_GET['edit']."' AND userid = '".$_SESSION['SESS_ID']."'";
        $result=mysql_query($query) or die;
	echo 'Update sucessful.';
	
} elseif (isset($_GET['edit'])) {
	$query=	"SELECT * FROM ".URL_TABLE." WHERE id = '".$_GET['edit']."' AND userid = '".$_SESSION['SESS_ID']."'";
	$result=mysql_query($query);
	$row=mysql_fetch_array($result);
	echo '<form name="form" action="?edit='.$_GET["edit"].'&submit" method="post">';
	echo '<b>Editting '.INSTALL_PATH.$row['id'].'</b><br />';
	echo '<input type="longurl" name="longurl" VALUE="'.$row['url'].'"/><br />';
	echo '<label for="disable">Disable URL:</label></td><td><input type="checkbox" name="disabled" VALUE="1"'; if($row['disabled']==1) {echo 'CHECKED';} echo '>';
	echo '<br /><input type="submit" name="Submit" value="Submit" />';
	echo '</form>';
} elseif ( (isset($_POST['sortitem'])) || (isset($_POST['sortorder'])) || (isset($_POST['limit'])) ) {
	$_SESSION['sortitem']=mysql_real_escape_string(strip_tags($_POST['sortitem']));
        $_SESSION['sortorder']=mysql_real_escape_string(strip_tags($_POST['sortorder']));
        $_SESSION['limit']=mysql_real_escape_string(strip_tags($_POST['limit']));
}

	
//\?\>
//
//<form name="form" action="search.php" method="get">
//  <input type="text" name="q" />
//  <input type="submit" name="Submit" value="Search" />
//</form>
//<?php
  // Get the search variable from URL

if ( (!isset($_GET['submit'])) && (isset($_GET['edit']))) {
} else {
// rows to return
$limit=$_SESSION['limit']; 

// Build SQL Query  
 $query = "select * from ".URL_TABLE." where userid = ".$_SESSION['SESS_ID']." ORDER BY ".$_SESSION['sortitem']." ".$_SESSION['sortorder'];

 $results=mysql_query($query);
 $numrows=mysql_num_rows($results);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
  }

// next determine if s has been passed to script, if not use 0
//$s = $_GET['s'];
  if (empty($s)) {
  $s=0;
  }

// begin to show results set
$count = 1 + $s ;

// now you can display thse results returned
?>
<br /><form id="sortform" name="sortform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Sort by: <select name='sortitem' onchange='this.form.submit()'>
  <option <?PHP if ($_SESSION['sortitem']=='id') {echo ' SELECTED '; } ?>value="id">Short URL</option>
  <option <?PHP if ($_SESSION['sortitem']=='createddate') {echo ' SELECTED '; } ?>value="createddate">Created Date</option>
  <option <?PHP if ($_SESSION['sortitem']=='visited') {echo ' SELECTED '; } ?>value="visited"># of Clicks</option>
  <option <?PHP if ($_SESSION['sortitem']=='url') {echo ' SELECTED '; } ?>value="url">Long URL</option>
  <option <?PHP if ($_SESSION['sortitem']=='disabled') {echo ' SELECTED '; } ?>value="disabled">Disabled</option>
</select>
Sort order: <select name='sortorder' onchange='this.form.submit()'>
  <option <?PHP if ($_SESSION['sortorder']=='') {echo ' SELECTED '; } ?>value="">Ascending</option>
  <option <?PHP if ($_SESSION['sortorder']=='DESC') {echo ' SELECTED '; } ?>value="DESC">Descending</option>
</select>
# of URLs per page: <select name='limit' onchange='this.form.submit()'>
  <option <?PHP if ($_SESSION['limit']=='10') {echo ' SELECTED '; } ?>value="10">10</option>
  <option <?PHP if ($_SESSION['limit']=='20') {echo ' SELECTED '; } ?>value="20">20</option>
  <option <?PHP if ($_SESSION['limit']=='30') {echo ' SELECTED '; } ?>value="30">30</option>
  <option <?PHP if ($_SESSION['limit']=='40') {echo ' SELECTED '; } ?>value="40">40</option>
  <option <?PHP if ($_SESSION['limit']=='50') {echo ' SELECTED '; } ?>value="50">50</option>
  <option <?PHP if ($_SESSION['limit']=='60') {echo ' SELECTED '; } ?>value="60">60</option>
  <option <?PHP if ($_SESSION['limit']=='70') {echo ' SELECTED '; } ?>value="70">70</option>
  <option <?PHP if ($_SESSION['limit']=='80') {echo ' SELECTED '; } ?>value="80">80</option>
  <option <?PHP if ($_SESSION['limit']=='90') {echo ' SELECTED '; } ?>value="90">90</option>
  <option <?PHP if ($_SESSION['limit']=='100') {echo ' SELECTED '; } ?>value="100">100</option>
</select></form>
<table><tr><th>#</th><th></th><th align="left">Short URL</th><th align="left">Long URL</th><th># of Visits</th><th>Disabled</th></tr>
<?PHP
  mysql_data_seek($results,$s);
  while ($count<=$s+$limit && $count<=$numrows) {
  	$row= mysql_fetch_array($results);
  	$url = $row["url"];
  	$shortid = $row["id"];
	$visits = $row["visited"];
	if ($row['disabled']==0) { $disabled='N'; } else { $disabled='Y'; }
	echo "<tr><td>$count.)&nbsp;</td><td><a href=\"?edit=".$row['id']."\">edit</a>&nbsp;-&nbsp;</td><td><a href=\"".INSTALL_PATH."$shortid\">".INSTALL_PATH."$shortid</a></td><td>$url</td><td align=\"center\">$visits</td><td align=\"center\">$disabled</td></tr>" ;
	$count++ ;
	}
  echo '</table>';

  $currPage = (($s/$limit) + 1);

//break before paging
	echo "<br />";

// next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  	$prevs=($s-$limit);
  	print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
  	Prev $limit</a>&nbsp&nbsp;";
  	}

// calculate number of pages needing links
  $pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link
  $news=$s+$limit;

  echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next $limit &gt;&gt;</a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
}  
?>
<?php include 'includes/footer.php'; ?>
</body>
</html>
